using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Data;
using System.Linq;

namespace DataViewer
{
    public class FormDataDetail : Form
    {
        private readonly Config config;
        private readonly string fileName;
        private SplitContainer splitContainer1;
        private DataGridView dataGridView1;
        private FlowLayoutPanel flowLayoutPanel1;
        private Label labelTip;

        public FormDataDetail(Config config, string fileName)
        {
            this.config = config;
            this.fileName = fileName;
            InitializeComponent();
            this.Text = $"详情 - {Path.GetFileNameWithoutExtension(fileName)}";
            LoadData();
        }

        private void InitializeComponent()
        {
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
            this.labelTip = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.splitContainer1.SuspendLayout();
            this.SuspendLayout();
            // 
            // splitContainer1
            // 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.Location = new System.Drawing.Point(0, 0);
            this.splitContainer1.Name = "splitContainer1";
            this.splitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal;
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.dataGridView1);
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this.flowLayoutPanel1);
            this.splitContainer1.Size = new System.Drawing.Size(800, 600);
            this.splitContainer1.SplitterDistance = 200;
            this.splitContainer1.TabIndex = 0;
            // 
            // dataGridView1
            // 
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.AllowUserToDeleteRows = false;
            this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            this.dataGridView1.BackgroundColor = System.Drawing.SystemColors.Window;
            this.dataGridView1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dataGridView1.Location = new System.Drawing.Point(0, 0);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.RowHeadersVisible = false;
            this.dataGridView1.RowTemplate.Height = 23;
            this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.Size = new System.Drawing.Size(800, 200);
            this.dataGridView1.TabIndex = 0;
            // 
            // flowLayoutPanel1
            // 
            this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 0);
            this.flowLayoutPanel1.Name = "flowLayoutPanel1";
            this.flowLayoutPanel1.Size = new System.Drawing.Size(800, 396);
            this.flowLayoutPanel1.TabIndex = 0;
            // 
            // labelTip
            // 
            this.labelTip.AutoSize = true;
            this.labelTip.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.labelTip.Font = new System.Drawing.Font("Microsoft YaHei", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.labelTip.ForeColor = System.Drawing.Color.Gray;
            this.labelTip.Location = new System.Drawing.Point(0, 580);
            this.labelTip.Name = "labelTip";
            this.labelTip.Size = new System.Drawing.Size(800, 20);
            this.labelTip.TabIndex = 1;
            this.labelTip.Text = "提示：单击图片可查看大图";
            this.labelTip.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // FormDataDetail
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 600);
            this.Controls.Add(this.labelTip);
            this.Controls.Add(this.splitContainer1);
            this.Name = "FormDataDetail";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
            this.splitContainer1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private void LoadData()
        {
            try
            {
                // 加载CSV内容
                string csvPath = Path.Combine(config.CsvFolder, fileName);
                if (File.Exists(csvPath))
                {
                    // 读取CSV文件
                    string[] lines = File.ReadAllLines(csvPath);
                    if (lines.Length > 0)
                    {
                        // 获取列名
                        string[] headers = lines[0].Split(',');
                        
                        // 创建DataTable
                        DataTable dt = new DataTable();
                        foreach (string header in headers)
                        {
                            dt.Columns.Add(header.Trim());
                        }

                        // 添加数据行
                        for (int i = 1; i < lines.Length; i++)
                        {
                            string[] values = lines[i].Split(',');
                            dt.Rows.Add(values.Select(v => v.Trim()).ToArray());
                        }

                        // 绑定到DataGridView
                        dataGridView1.DataSource = dt;
                    }
                }

                // 加载图片
                foreach (var folder in config.ImageFolders)
                {
                    string imgPath = Path.Combine(folder, Path.ChangeExtension(fileName, ".jpg"));
                    if (File.Exists(imgPath))
                    {
                        PictureBox pb = new PictureBox
                        {
                            Image = Image.FromFile(imgPath),
                            SizeMode = PictureBoxSizeMode.Zoom,
                            Width = 200,
                            Height = 200,
                            Tag = imgPath,
                            BorderStyle = BorderStyle.FixedSingle,
                            BackColor = Color.White,
                            Padding = new Padding(5),
                            Cursor = Cursors.Hand
                        };

                        // 添加鼠标悬停效果
                        pb.MouseEnter += (s, e) =>
                        {
                            pb.BorderStyle = BorderStyle.Fixed3D;
                            pb.BackColor = Color.LightGray;
                        };

                        pb.MouseLeave += (s, e) =>
                        {
                            pb.BorderStyle = BorderStyle.FixedSingle;
                            pb.BackColor = Color.White;
                        };

                        pb.Click += PictureBox_Click;
                        flowLayoutPanel1.Controls.Add(pb);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载数据时出错：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void PictureBox_Click(object sender, EventArgs e)
        {
            if (sender is PictureBox pb && pb.Tag is string imgPath)
            {
                using (var imgForm = new Form())
                {
                    var fullSizePb = new PictureBox
                    {
                        Image = Image.FromFile(imgPath),
                        Dock = DockStyle.Fill,
                        SizeMode = PictureBoxSizeMode.Zoom
                    };
                    imgForm.Controls.Add(fullSizePb);
                    imgForm.WindowState = FormWindowState.Maximized;
                    imgForm.Text = Path.GetFileNameWithoutExtension(imgPath);

                    // 添加提示标签
                    var labelTip = new Label
                    {
                        Text = "单击图片退出",
                        Dock = DockStyle.Bottom,
                        TextAlign = ContentAlignment.MiddleCenter,
                        Font = new Font("Microsoft YaHei", 10F, FontStyle.Regular),
                        ForeColor = Color.Gray,
                        Height = 30
                    };
                    imgForm.Controls.Add(labelTip);

                    // 添加点击事件处理
                    fullSizePb.Click += (s, ev) => imgForm.Close();

                    imgForm.ShowDialog();
                }
            }
        }
    }
} 